package assignment1;

import java.util.Random;

public class ClientTester{
	private static String[] phrases = new String[]{
		"Hello my friend!",
		"What you looking at?",
		"What are you doing?",
		"What is your favourite colour?",
		"Are you a boy or a girl?",
		"Beer or wine?",
		"Are you thirsty?",
		"Do you like coffee?",
		"When I die do you want all my money?",
		"The sun is shining!",
		"I'm thirsty",
		"Do you like ITU?"
	};
	private static Random rnd = new Random();
	
	private Client client;
	private String testerName;
	
	public ClientTester(Client client, String testerName){
		this.client = client;
		this.testerName = testerName;
	}
	
	/**
	 * Run the test.
	 * Sends 10 message to the server and checks whether or not
	 * the returned message has been converted to the case specified.
	 */
	public void run(){
		for(int i = 0; i < 10; i++){
			String phrase = "[" + testerName + "] " + randomPhrase();
			boolean upperCase = randomCase();
			
			client.sendMessage(phrase, upperCase);
			
			String phraseReturned = client.readMessage();
			
			System.out.println("Message returned should be " + (upperCase ? "uppercase" : "lowercase"));
			System.out.println("Message returned was: " + phraseReturned);
			System.out.println("Did casing match? " + ((upperCase ? phrase.toUpperCase() : phrase.toLowerCase()).equals(phraseReturned) ? "Yes" : "No"));
			System.out.println();
			try {
				Thread.sleep(rand(250, 750));
			} catch (Exception e) {}
		}
	}
	
	/**
	 * Get a random phrase from the array of phrases.
	 * @return A random phrase.
	 */
	private String randomPhrase(){
		return phrases[rand(0, phrases.length - 1)];
	}
	
	/**
	 * Get a random case, uppercase = true, lowercase = false.
	 * @return A random case.
	 */
	private boolean randomCase(){
		return (rand(0, 1) > 0);
	}
	
	/**
	 * Get a random number between min and max.
	 * @param min The minimum number (inclusive).
	 * @param max The maximum number (inclusive).
	 * @return A random number between min and max
	 */
	private static int rand(int min, int max){
		return rnd.nextInt(max - min + 1) + min;
	}
}
